package com.pea;

/**
 * @author: xiew72119
 * @Description:
 * @since 2025-06-27 14:31
 */
public class ThreadLocalTest {

    public static void main(String[] args) throws InterruptedException
    {
        ThreadLocal<String> threadLocal = new ThreadLocal<>();
        threadLocal.set("main");
        System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());//main:main

        Thread thread1 = new Thread(()->{
            threadLocal.set("thread1");
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());//Thread-0:thread1
        });

        Thread thread2 = new Thread(()->{
            threadLocal.set("thread2");
            System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());//Thread-1:thread2
        });

        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());//main:main

        threadLocal.remove();

        System.out.println(Thread.currentThread().getName() + ":" + threadLocal.get());//main:null


    }
}
